package com.jwds.digital.bios.business.core.utils;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>BeltCloudMathUtil</p>
 *
 * @author 林柏云
 * @Date：2024/11/27 16:34
 */
public class BeltCloudMathUtil {

  /**
   * 计算方差
   * @param numbers
   * @return
   */
  public static BigDecimal calculateVariance(List<BigDecimal> numbers)  {
    BigDecimal mean = BigDecimal.ZERO;
    BigDecimal sumOfSquaredDiffs = BigDecimal.ZERO;

    for (BigDecimal number : numbers) {
      mean = mean.add(number);
    }
    mean = mean.divide(BigDecimal.valueOf(numbers.size()), 2, BigDecimal.ROUND_HALF_UP);

    for (BigDecimal number : numbers) {
      sumOfSquaredDiffs = sumOfSquaredDiffs.add(number.subtract(mean).pow(2));
    }

    sumOfSquaredDiffs = sumOfSquaredDiffs.divide(BigDecimal.valueOf(numbers.size()), 2, BigDecimal.ROUND_HALF_UP);
    return sumOfSquaredDiffs;
  }
}
